package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ExamTestMapper;
import com.think.teaching.model.ExamPaper;
import com.think.teaching.model.ExamTest;
import com.think.teaching.service.ExamTestService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ExamTestServiceImpl extends BaseServiceImpl<ExamTestMapper, ExamTest> implements ExamTestService {

	@Override
	public ExamTest AddExamTest(ExamTest examTest) {
		if (this.save(examTest)) {
			return examTest;
		}
		return null;
	}


	@Override
	public ExamTest UpdateExamTest(ExamTest examTest) {
		if (this.updateById(examTest)) {
			return examTest;
		}
		return null;
	}

	@Override
	public ExamTest GetExamTest(Integer id) {
		QueryWrapper<ExamTest> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	
	public List<ExamTest> GetExamTestValidListByCourseId(Integer courseId){
		return this.baseMapper.getExamTestValidListByCourseId(courseId);
	}
	public IPage<Map<String, Object>> ListExamTestWithTagAndType(Integer count, Integer page, Integer type, Integer tag,
			Integer state,Integer id) {
		
		IPage<Map<String, Object>> ipage = new Page<>(page, count);
		return baseMapper.ListExamTest(ipage,state,type,tag,id);
	}

	@Override
	public List<ExamTest> SearchExamTestWithKey(String key, Boolean withoutBan) {
		QueryWrapper<ExamTest> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("exam_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public ExamTest SetExamTestState(Integer id, Integer state) {
		UpdateWrapper<ExamTest> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
	
	public Map<String, Object> GetExamPaperWithQuestions(Integer id){
		return this.baseMapper.getExamPaperWithQuestions(id);
	}
	
	
	public IPage<Map<String, Object>> ListFinishedExamTest(Integer page,Integer count,Integer course){
		IPage<Map<String, Object>> iPage = new Page<>(page, count);
		return baseMapper.ListFinishedExamTest(iPage,course);
	}
	
	/**
	 * 删除和指定的paperId相关的考试
	 * @param paperId
	 * @return
	 */
	public Boolean DeleteRecordsByPaperId(Integer paperId) {
		UpdateWrapper<ExamTest> queryWrapper = new UpdateWrapper<ExamTest>();
		queryWrapper.eq("paper_id", paperId).set("delflag", 1);
		return this.update(queryWrapper);
	}
}
